//The MIT License

//Copyright (c) 2009 Lluis Sanchez, Nick Darnell

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.

using System;
using System.Reflection.Emit;

using Polymorph.Properties;

namespace Polymorph
{
    /// <summary>
    /// The <see cref="CodeFor"/> class represents a for loop.
    /// </summary>
    public class CodeFor : CodeStatement
    {
        private CodeExpression m_initExp;
        private CodeExpression m_conditionExp;
        private CodeExpression m_nextExp;

        private CodeBlock m_forBlock;

        /// <summary>
        /// Initializes a new instance of <see cref="CodeFor"/>.
        /// </summary>
        /// <param name="initExp">The initialize expression for the loop.</param>
        /// <param name="conditionExp">The condition to test for the loop.</param>
        /// <param name="nextExp">The increment/next expression for the loop.</param>
        public CodeFor(CodeExpression initExp, CodeExpression conditionExp, CodeExpression nextExp)
        {
            m_initExp = initExp;
            m_conditionExp = conditionExp;
            m_nextExp = nextExp;

            if (conditionExp.ResultType != typeof(bool))
            {
                throw new ArgumentException(String.Format(Resources.ExpMustHave0Result,
                    "bool"), "condition");
            }
        }

        /// <summary>
        /// The block associated with the for loop.
        /// </summary>
        public CodeBlock ForBlock
        {
            get { return m_forBlock; }
            set { m_forBlock = value; }
        }

        /// <summary>
        /// Generates the IL for the for loop.
        /// </summary>
        /// <param name="gen">The IL generator to use.</param>
        public override void Generate(ILGenerator gen)
        {
            CodeBlock block = new CodeBlock();
            CodeWhile cw;

            block.Add(m_initExp);
            block.Add(cw = new CodeWhile(m_conditionExp));
            CodeBlock loopBlock = new CodeBlock();
            loopBlock.Add(m_forBlock);
            loopBlock.Add(m_nextExp);
            cw.WhileBlock = loopBlock;

            block.Generate(gen);
        }

        /// <summary>
        /// Writes human readable code for the for loop.
        /// </summary>
        /// <param name="cw">The code writer to use.</param>
        public override void PrintCode(CodeWriter cw)
        {
            cw.Write("for (");
            m_initExp.PrintCode(cw);
            cw.Write(";");
            m_conditionExp.PrintCode(cw);
            cw.Write(";");
            m_nextExp.PrintCode(cw);
            cw.Write(") {");
            cw.EndLine();
            cw.Indent();
            m_forBlock.PrintCode(cw);
            cw.Unindent();
            cw.BeginLine().Write("}");
        }
    }
}